package com.example.myapplication

import android.app.Application
import android.util.Log
import com.example.myapplication.extensions.appConfig
import com.example.myapplication.net.ApiNetwork
import com.example.myapplication.ui.FileRoomDatabase
import com.example.myapplication.utils.Logs
import com.saop.core.SAOP
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import java.util.*
/**
 * App的application
 */
class App : Application(), AppManager.OnAppForegroundStateChangeListener {
    companion object {
        lateinit var instance: App
    }

    override fun onCreate() {
        super.onCreate()
        Logs.init("Atom")
        SAOP.init(this)
        SAOP.debug(true)
        instance = this
        FileRoomDatabase.getDatabase(this)
        AppManager.getInstance().addListener(this);
    }

    val applicationScope = CoroutineScope(SupervisorJob())

    val apiNetwork = ApiNetwork()

    val database by lazy { FileRoomDatabase.getDatabase(this) }
    /**
     * 获取Token
     */
    fun getToken(): String? {
        return appConfig.token
    }
    /**
     * 获取user Id
     */
    fun getUserId(): Int {
        return appConfig.userId
    }
    /**
     * 获取是否在前台
     */
    fun getForeGround(): Boolean {
        return appConfig.isForeGround
    }
    /**
     * App的监听
     */
    override fun onAppForegroundStateChange(newState: AppManager.AppForegroundState?) {
        appConfig.isForeGround = AppManager.AppForegroundState.IN_FOREGROUND == newState
    }
}